package com.poizon.infrastructure.center.config.core.event.listener;


import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.poizon.infrastructure.center.config.core.event.strategy.ConfigChangeEventStrategy;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;

/**
 * 配置中心/配置变更操作实现
 */
@Slf4j
public class ConfigChangeListenerImpl extends AbstractChangeListener {
    @Override
    protected Executor getExecutorImpl() {
        return null;
    }

    @Override
    protected void handlerConfigInfoImpl(Properties properties) {
        log.info(">配置变更={}", JSON.toJSONString(properties));

        // 获取配置变更策略实现
        Map<String, ConfigChangeEventStrategy> beansOfType = SpringUtil.getBeansOfType(ConfigChangeEventStrategy.class);

        if (beansOfType == null || beansOfType.isEmpty()) {
            log.info(">未配置配置变更策略实现");
            return;
        }

        beansOfType.values().stream().filter(s -> s.support(properties)).forEach(s -> {
            s.configChange(properties);
        });
    }
}
